<?php

namespace Profile;

use C\L\WebUserController;
use C\M\User;
use C\M\CouponReceive;
use Phalcon\Paginator\Factory;

class UserController extends WebUserController
{
    public function pointsAction()
    {
        $currentPage = $this->request->getQuery('page', 'int', 1);
        $perPage     = $this->request->getQuery('per_page', 'int', 20);
        $userId = $this->uid;

        $builder = $this
            ->modelsManager
            ->createBuilder()
            ->columns(['id', 'title', 'money', 'status'])
            ->from('C\M\UserFunds')
            ->andWhere('type = :type:', ['type' => 'credit'])
            ->andWhere('uid = :user_id:', ['user_id' => $userId])
            ->orderBy('id DESC');

        $result = $this->paginationByBuilder($builder, $currentPage, $perPage);

        return $this->success($result);
    }

    public function profitAction()
    {
        $data = [
            'received_profit'   => $this->s_itemaprlist->getSum('apr_money', ['uid' => $this->uid, 'status' => 'Y']),
            'unreceived_profit' => $this->s_itemaprlist->getSum('apr_money', ['uid' => $this->uid, 'status' => 'D']),
            'unreceived_money'  => $this->s_itemaprlist->getSum('money', ['uid' => $this->uid, 'status' => 'D']),
        ];

        return $this->success($data);
    }

    public function couponsAction()
    {
        $amount = $this->request->getQuery('amount', 'int', PHP_INT_MAX);
        $type   = $this->request->getQuery('type', 'string', 'available');
        $coupons = $type == 'available'
            ? CouponReceive::availableCoupons($this->uid, $amount)
            : CouponReceive::unavailableCoupons($this->uid, $amount);

        $this->success($coupons);
    }

    public function balanceTypesAction()
    {
        $tabs = $this->s_funds->getTabs();
        $result = array_map(function ($key) use ($tabs) {
            return [
                'value' => $key,
                'text'  => $tabs[$key]['text'],
            ];
        }, array_keys($tabs));

        $this->success($result);
    }

    public function balanceAction()
    {
        $currentPage = $this->request->getQuery('page', 'int', 1);
        $perPage     = $this->request->getQuery('per_page', 'int', 20);
        $tab         = $this->request->getQuery('tab', 'string', null);

        $userId = $this->uid;

        $builder = $this
            ->modelsManager
            ->createBuilder()
            ->columns(['id', 'title', 'money', 'btype' ,'stype', 'status', 'addtime'])
            ->from('C\M\UserFunds')
            ->andWhere('type = :type:', ['type' => 'money'])
            ->andWhere('uid = :user_id:', ['user_id' => $userId])
            ->orderBy('id DESC');

        if ($tab) {
            $tabs = $this->s_funds->getTabs();
            $stypes = $tabs[$tab]['stypes'];
            $builder = $builder->inWhere('stype', $stypes);
        }

        $result = $this->paginationByBuilder($builder, $currentPage, $perPage);

        return $this->success($result);
    }
}
